define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'depot/storage/index' + location.search,
                    add_url: 'depot/storage/add',
                    del_url: 'depot/storage/del',
                    multi_url: 'depot/storage/multi',
                    import_url: 'depot/storage/import',
                    product_url: 'depot.storage.product/index',
                    review_url: 'depot/storage/review',
                    warehouse_url: 'depot/storage/warehouse',
                    back_url: 'depot/back/edit',
                    backinfo_url: 'depot/back/index',
                    table: 'depot_storage',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'supplier.name', title: __('Supplier'),searchList:Config.supplierList},
                        {field: 'type', title: __('Type'), searchList: {'1' : __('Direct'), '2' : __('Back')}, formatter: Table.api.formatter.normal},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {'0' : __('ApprovalPending'), '1' : __('Failure'), '2' : __('StockPending'), '3' : __('Completed')}, formatter: Table.api.formatter.status},
                        {field: 'adminid', title: __('Adminid')},
                        {field: 'reviewerid', title: __('Reviewerid')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                            {
                                name: 'product',
                                title: '入库商品',
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-success btn-dialog",
                                icon: 'fa fa-eye',
                                url: $.fn.bootstrapTable.defaults.extend.product_url,
                            },
                            {
                                name: 'review',
                                title: '审核',
                                // extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-success btn-click",
                                icon: 'fa fa-search',
                                // url: $.fn.bootstrapTable.defaults.extend.review_url,
                                visible: function (row) {
                                    return row.status == 0;
                                },
                                // success: ()=>$(".btn-refresh").trigger("click"),
                                click:(value,row,index)=>{
                                    var ids = row.ids
                                    console.log(row)
                                    layer.confirm('审核是否通过？', {title: '审核', btn: ['通过', '失败']}, 
                                    function(index){
                                        // index 是窗口的id
                                        review(ids,2)
                                        layer.close(index)
                                    },
                                    function(index){
                                        review(ids,1)
                                        layer.close(index)
                                    })
                                }
                            },
                            {
                                name: 'warehouse',
                                title: '入库完成',
                                classname: "btn btn-xs btn-success btn-click",
                                icon: 'fa fa-home',
                                visible: function (row) {
                                    return (row.status==2&&row.type==1)||(row.status==2&&row.type==2&&row.isback);
                                },
                                click:(value,row,index)=>{
                                    var ids = row.ids
                                    layer.confirm('是否入库完成？', {title: '入库完成', btn: ['确认', '取消']}, 
                                    function(index){
                                        // index 是窗口的id
                                        warehouse(ids,3)
                                        layer.close(index)
                                    })
                                }
                            },
                            {
                                name: 'back',
                                title: '退货',
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-info btn-dialog",
                                icon: 'fa fa-truck',
                                url: $.fn.bootstrapTable.defaults.extend.back_url,
                                visible: function (row) {
                                    return row.status == 2&&row.type==2 && !row.isback;
                                },
                            },
                            {
                                name: 'backinfo',
                                title: '退货信息',
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-info btn-dialog",
                                icon: 'fa fa-eye',
                                url: $.fn.bootstrapTable.defaults.extend.backinfo_url,
                                visible: function (row) {
                                    return row.status == 2&&row.type==2 && row.isback;
                                },
                            },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //给自定义按钮绑定弹框
            $(document).on('click', '.review', function(){
                //获取选中的ids
                var ids = Table.api.selectedids(table);
                //弹出确认对框
                layer.confirm('审核是否通过？', {title: '审核', btn: ['通过', '失败']}, 
                function(index){
                    // index 是窗口的id
                    review(ids,2)
                    layer.close(index)
                },
                function(index){
                    review(ids,1)
                    layer.close(index)
                })
            })
            function review(ids,status)
            {
                Fast.api.ajax(
                    $.fn.bootstrapTable.defaults.extend.review_url + `?ids=${ids}&status=${status}`,
                    function(data, success) //成功回调函数 success
                    {
                        // 刷新动作
                        $(".btn-refresh").trigger('click');
                    }
                );
            }
            function warehouse(ids,status)
            {
                Fast.api.ajax(
                    $.fn.bootstrapTable.defaults.extend.warehouse_url + `?ids=${ids}&status=${status}`,
                    function(data, success) //成功回调函数 success
                    {
                        // 刷新动作
                        $(".btn-refresh").trigger('click');
                    }
                );
            }
            $(document).on('click', '.warehouse', function(){
                //获取选中的ids
                var ids = Table.api.selectedids(table);
                //弹出确认对框
                layer.confirm('是否入库完成？', {title: '入库完成', btn: ['确认', '取消']}, 
                function(index){
                    // index 是窗口的id
                    warehouse(ids,3)
                    layer.close(index)
                })
            })
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'depot/storage/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'depot/storage/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'depot/storage/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        review: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
